package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.mall.entity.admin.MallProductRecommendEntity;
import net.lahuobao.mall.entity.admin.MallProductRecommendHistoryEntity;
import net.lahuobao.mall.service.admin.MallProductRecommendHistoryServiceI;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.util.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.lahuobao.mall.service.admin.MallProductRecommendServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Service("mallProductRecommendService")
@Transactional
public class MallProductRecommendServiceImpl extends CommonServiceImpl implements MallProductRecommendServiceI {

    @Autowired
    private MallProductRecommendHistoryServiceI historyService;

    @Override
    public void save(MallProductRecommendEntity mallProductRecommendEntity) {
        //处理逻辑
        CriteriaQuery cq = new CriteriaQuery(MallProductRecommendEntity.class);
        //判断该类型顺序是否重复
        cq.eq("recommendtype", mallProductRecommendEntity.getRecommendtype());
        cq.eq("sort", mallProductRecommendEntity.getSort());
        cq.add();
        List<MallProductRecommendEntity> list = super.getListByCriteriaQuery(cq, false);
        if(list.size() > 0){//有重复就插入先把原来的插入历史表中
            for(MallProductRecommendEntity entity : list){
                MallProductRecommendHistoryEntity historyEntity = new MallProductRecommendHistoryEntity();
                try {
                    MyBeanUtils.copyBean2Bean(historyEntity, entity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                historyService.save(historyEntity);
                super.delete(entity);
            }
        }
        super.save(mallProductRecommendEntity);
    }
}